#include "import_map_service.h"
#include "../database/database.h"
#include "../database/place_db.h"
#include "../sqlite3.h"
#include <iostream>
#include <vector>
#include <string>

// 添加UTF-8支持
#if defined(_MSC_VER) && defined(_WIN32)
#pragma execution_character_set("utf-8")
#endif

// Import places data command line tool
int main(int argc, char* argv[]) {
    if (argc < 2) {
        std::cout << "Usage: " << argv[0] << " <database_file_path> [CSV_file_path]" << std::endl;
        std::cout << "Example: " << argv[0] << " data/tour.db data/beijing_places.csv" << std::endl;
        return 1;
    }
    
    std::string dbPath = argv[1];
    std::string csvPath = (argc > 2) ? argv[2] : "data/beijing_places.csv";
    
    // Initialize database
    Database db;
    if (!db.open(dbPath)) {
        std::cerr << "Cannot open database: " << dbPath << std::endl;
        return 1;
    }
    
    // Ensure places table exists
    std::string createPlacesTable = 
        "CREATE TABLE IF NOT EXISTS places ("
        "id INTEGER PRIMARY KEY AUTOINCREMENT, "
        "name TEXT NOT NULL, "
        "type TEXT NOT NULL, "
        "latitude REAL NOT NULL, "
        "longitude REAL NOT NULL, "
        "outline TEXT, "
        "description TEXT, "
        "address TEXT);";
    
    std::string createInternalPlacesTable = 
        "CREATE TABLE IF NOT EXISTS internal_places ("
        "id INTEGER PRIMARY KEY AUTOINCREMENT, "
        "parent_id INTEGER NOT NULL, "
        "name TEXT NOT NULL, "
        "type TEXT NOT NULL, "
        "latitude REAL NOT NULL, "
        "longitude REAL NOT NULL, "
        "description TEXT, "
        "FOREIGN KEY (parent_id) REFERENCES places (id) ON DELETE CASCADE);";
    
    char* errMsg = nullptr;
    if (sqlite3_exec(db.getConnection(), createPlacesTable.c_str(), nullptr, nullptr, &errMsg) != SQLITE_OK) {
        std::cerr << "Failed to create places table: " << errMsg << std::endl;
        sqlite3_free(errMsg);
        return 1;
    }
    
    if (sqlite3_exec(db.getConnection(), createInternalPlacesTable.c_str(), nullptr, nullptr, &errMsg) != SQLITE_OK) {
        std::cerr << "Failed to create internal_places table: " << errMsg << std::endl;
        sqlite3_free(errMsg);
        return 1;
    }
    
    // Create place database object
    PlaceDB placeDb(db);
    
    // Create map service object (using your Baidu Maps API key)
    ImportMapService mapService("YWYhzv8agmWHMH3YXoHBAJIIDQfiUAzY");
    
    // Create CSV template if needed
    if (argc <= 2) {
        std::cout << "Do you need to create a basic place CSV template? (y/n): ";
        char choice;
        std::cin >> choice;
        
        if (choice == 'y' || choice == 'Y') {
            if (ImportMapService::createBasicPlacesCSVTemplate(csvPath)) {
                std::cout << "CSV template created: " << csvPath << std::endl;
                std::cout << "Please edit this file to add or modify place data, then run this tool again." << std::endl;
                return 0;
            } else {
                std::cerr << "Failed to create CSV template!" << std::endl;
                return 1;
            }
        }
    }
    
    // Define place types to import
    std::vector<std::string> placeTypes = {
        "Attraction", "School", "Restaurant", "Shop", "Restroom", "Library", "Canteen", "Supermarket", "Cafe"
    };
    
    // Execute mixed import
    std::cout << "Starting place data import..." << std::endl;
    if (mapService.importPlacesMixedMethod(placeDb, csvPath, placeTypes, 3)) {
        std::cout << "Place data import successful!" << std::endl;
    } else {
        std::cerr << "Place data import failed!" << std::endl;
        return 1;
    }
    
    return 0;
}
